package com.sarnath.sardoop.modules.hbase.web;

import javax.annotation.Resource;

import org.apache.hadoop.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sarnath.sardoop.common.bean.Response;
import com.sarnath.sardoop.common.web.BaseController;
import com.sarnath.sardoop.modules.hbase.entity.QueryCondition;
import com.sarnath.sardoop.modules.hbase.service.HbaseDdlService;
import com.sarnath.sardoop.modules.hbase.service.HbaseQueryService;
import com.sarnath.sardoop.modules.sys.util.UserUtils;

@Controller
@RequestMapping(value = "${adminPath}/hbase")
public class HbaseController extends BaseController {
	@Resource
	HbaseQueryService hbaseQuery;
	@Resource
	HbaseDdlService hbaseDdl;
	
	@RequestMapping(value="query")
	@ResponseBody
	public Response query(@ModelAttribute QueryCondition condition){
		Response res=new Response();
		try {
			condition.setTableName(StringUtils.join(":", new String[]{UserUtils.getNameSpace(),condition.getTableName()}));
			res.put("result",hbaseQuery.queryMap(condition));
		} catch (Exception e) {
			res.setStatus(1);
			res.setMsg(e.toString());
			logger.info("",e);
		}
		return res;
	}
	
	@RequestMapping(value="createTable")
	@ResponseBody
	public Response createTable(@RequestParam String tableName,@RequestParam String... family){
		Response res=new Response();
		try {
			hbaseDdl.createTable(StringUtils.join(":", new String[]{UserUtils.getNameSpace(),tableName}), family);
		} catch (Exception e) {
			res.setStatus(1);
			res.setMsg(e.toString());
			logger.info("",e);
		}
		return res;
	}
	@RequestMapping(value="dropTable")
	@ResponseBody
	public Response dropTable(@RequestParam String tableName){
		Response res=new Response();
		try {
			hbaseDdl.dropTable(StringUtils.join(":", new String[]{UserUtils.getNameSpace(),tableName}));
		} catch (Exception e) {
			res.setStatus(1);
			res.setMsg(e.toString());
			logger.info("",e);
		}
		return res;
	}
	@RequestMapping(value="truncate")
	@ResponseBody
	public Response truncate(@RequestParam String tableName){
		Response res=new Response();
		try {
			hbaseDdl.truncate(StringUtils.join(":", new String[]{UserUtils.getNameSpace(),tableName}));
		} catch (Exception e) {
			res.setStatus(1);
			res.setMsg(e.toString());
			logger.info("",e);
		}
		return res;
	}
	@RequestMapping(value={"insert","update"},method=RequestMethod.POST)
	@ResponseBody
	public Response insert(@RequestParam String tableName,@RequestParam String family,@RequestParam String[] rowkey
			,@RequestParam String[] field,@RequestParam String[] value){
		Response res=new Response();
		try {
			if(rowkey.length>1000 || rowkey.length!=field.length || rowkey.length!=value.length) throw new RuntimeException("提交记录条数错误");
			hbaseDdl.insert(StringUtils.join(":", new String[]{UserUtils.getNameSpace(),tableName}),family,rowkey,field,value);
		} catch (Exception e) {
			res.setStatus(1);
			res.setMsg(e.toString());
			logger.info("",e);
		}
		return res;
	}
	@RequestMapping(value="delete",method=RequestMethod.POST)
	@ResponseBody
	public Response delete(@RequestParam String tableName,@RequestParam String family,
			@RequestParam(required=false) String field,@RequestParam String... rowkey){
		Response res=new Response();
		try {
			hbaseDdl.delete(StringUtils.join(":", new String[]{UserUtils.getNameSpace(),tableName}),family,field,rowkey);
		} catch (Exception e) {
			res.setStatus(1);
			res.setMsg(e.toString());
			logger.info("",e);
		}
		return res;
	}
}
